import { Component,OnInit } from '@angular/core';

import { Router } from '@angular/router';
import { Global } from 'src/global';
import { User } from './user/user';
import { Location } from '@angular/common';
import { SearchComponent } from './search/search.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  implements OnInit{
  
  title = 'pokemon_pv';
  public wd:string='';
  public stype:string='Id';
  user =new User();
  constructor(private router: Router,location: Location,) {
    router.events.subscribe(val => {  //监控url的变化    
      if(Global.user == null){
        this.user = new User();
      }else{
        this.user = Global.user; //更新登录用户信息
      }
    });

   }
  ngOnInit(): void {
    // console.log(Global.user)
    if(window.sessionStorage["user"] != null){ //已登录
      if(Global.user==null){  Global.user = new User(); }
      Global.user=JSON.parse(window.sessionStorage["user"]) ;
      
      this.user=Global.user
      // console.log(this.user.id)
      // console.log(Global.user)
      // console.log(Global.user.id)
    }else{ //未登录，返回登录界面
      this.user = new User();
      this.router.navigate(['/login'])
    }
  }
  async logoutConfirm() {
    this.logout();//执行退出方法
  }
  logout(){
    if(Global.user){
        window.sessionStorage.clear(); //如果判断用户未退出，则清空登录信息
        Global.user = new User();
        location.reload();
    }

  }


}
